
use "${dir_raw}/GSS7214_R4.dta", clear

* ------- this is for 1985, 1987, 2004 
*keep if year == 1985 | year == 1987 | year == 2004 | year == 1998 | year == 1986
tab year if missing(vstrat)
tab year if missing(vpsu)
table year, c(min sampcode max sampcode)

* ------- suvey time 
recode dateintv (missing=.)

gen str_dateintv = string(dateintv)
replace str_dateintv = "" if str_dateintv == "."

gen n_str = length(str_dateintv)

gen month = "" if n_str != 0
replace month = substr(str_dateintv,1,1) if n_str == 3
replace month = substr(str_dateintv,1,2) if n_str == 4

gen day = "" if n_str != 0
replace day = substr(str_dateintv, 2,2) if n_str == 3
replace day = substr(str_dateintv, 3,2) if n_str == 4

gen yy = year 

* change 2004 sample "month" -- january
replace yy = 2005 if year == 2004 & month == "1"

gen svydate = day+"/"+month+"/"+string(yy)
gen svydate2 = date(svydate, "DMY")
format svydate2 %td

* day of week
gen day_of_week = dow(svydate2)
gen wkend = (day_of_week==0 | day_of_week == 6) if ~missing(day_of_week)
gen week_of_month = week(svydate2)


* ----------- subject's baseline characteristics  
gen r_age = age if ~missing(age)
gen r_female = (sex==2) if ~missing(sex)
gen r_educ = educ if ~missing(educ)

gen r_race = race if ~missing(race)
	gen r_white = race==1 if ~missing(race)
	gen r_black = race==2 if ~missing(race)
	gen r_others = race==3 | race == 4 if ~missing(race)

gen r_marstat = marital 
	gen r_married = marital==1 if ~missing(marital)
	gen r_single = marital == 5 if ~missing(marital)

gen r_nchilds = childs if ~missing(childs)
gen r_adults = adults if ~missing(adults)

gen r_attendance = attend if ~missing(attend)
gen r_wrkstat = wrkstat
	recode r_wrkstat (1 2 =1) (else=0), gen(r_working)
	recode r_wrkstat (3 4=1) (else=0), gen(r_unemployed)
	recode r_wrkstat (5=1) (else=0), gen(r_retired)
	recode r_wrkstat (6 7 8=1) (else=0), gen(r_otherworks)

gen r_sei = sei  if ~missing(sei)

* political information 
gen r_partyid = partyid if ~missing(partyid)
	gen r_partyid7 = partyid + 1 if partyid < 7

gen r_ideo = polviews if ~missing(polviews)

* ------------ subject's political interests 
gen r_pol_interest = 4 - polint1 
gen r_pol_interest1 = 5 - polint 
gen r_pol_interest2 = 4 - intpol 

gen r_pol_interest0 = r_pol_interest/3 
replace r_pol_interest0 = r_pol_interest1/4 if ~missing(r_pol_interest1)
replace r_pol_interest0 = r_pol_interest2/3 if ~missing(r_pol_interest2)

gen r_pol_discuss = 4- discpol 
replace r_pol_discuss = 4- talkpol 

gen r_pol_mem = mempolit == 1 if ~missing(mempolit)

* ----------- network information : important matters 
* network size 
gen n_size = numgiven if ~missing(numgiven)
gen isolated = n_size == 0 if ~missing(n_size)

* political discussion ntwork size 
gen a1_talkpol = 6 - talkpol1 
gen a2_talkpol = 6 - talkpol2 
gen a3_talkpol = 6 - talkpol3 

gen a1_partyid = partyid1 
gen a2_partyid = partyid2
gen a3_partyid = partyid3

tab partyid1,m, if partyid1 != .i

forvalues i = 1/5{
	gen a`i'_spouse = spouse`i'    ==1 if spouse`i' >0 & spouse`i' <9
	gen a`i'_parent = parent`i'    ==1 if parent`i' >0 & parent`i' <9
	gen a`i'_sibling = sibling`i'  ==1 if sibling`i' >0 & sibling`i' <9
	gen a`i'_child = child`i'      ==1 if child`i' >0 & child`i' <9
	gen a`i'_othfam = othfam`i'    ==1 if othfam`i' >0 & othfam`i' <9
	gen a`i'_cowork = cowork`i'    ==1 if cowork`i' >0 & cowork`i' <9
	gen a`i'_member =  memgrp`i'   ==1 if memgrp`i' > 0 & memgrp`i' <9 
	gen a`i'_neighbor = neighbr`i' ==1 if neighbr`i' >0 & neighbr`i' <9
	gen a`i'_friend = friend`i'    ==1 if friend`i' >0 & friend`i' <9
	gen a`i'_advisor = advisor`i'  ==1 if advisor`i' >0 & advisor`i' <9
	gen a`i'_other = other`i'      ==1 if other`i' >0 & other`i' <9
}

* survey sampling : sampcode wtssnr 
svyset sampcode [pw= wtssnr]

* before 2004 using adults
gen p_adults = 1/adults
gen wtall = p_adults if year < 2004
	replace wtall = wtssnr if year >= 2004

svyset sampcode [pw= wtall]

* ---------------- interview related variable
*lookfor interview
gen i_female = intsex == 2 if ~missing(intsex)
gen i_age = intage 

recode intethn (1=1) (2=2) (3/5=3), gen(i_race)
	gen i_black = i_race == 2 if ~missing(i_race)
	gen i_white = i_race == 1 if ~missing(i_race)
	gen i_others = i_race == 3 if ~missing(i_race)
gen i_tenure = intyrs 

gen i_uncoop = coop 
gen i_poorcomprend = comprend 
gen i_saq_skip_gene = (saqgene == 3) if ~missing(saqgene)

sort year intid svydate2 id 
bysort year intid: gen i_numskip = sum(i_saq_skip_gene[_n-1])
recode i_numskip (3=2) (4/24=3), gen(i_numskip_c)

gen n_size_miss = (numgiven==.i) if year == 2004

gen i_ballot = ballot 
	gen i_ballot_a = ballot == 1 if ~missing(ballot)
	gen i_ballot_b = ballot == 2 if ~missing(ballot)
	gen i_ballot_c = ballot == 3 if ~missing(ballot)

egen i_saq_skip_all = rowtotal(i_saq_skip_*)
gen i_phonemode = mode==2 if ~missing(mode)
gen i_length = lngthinv

gen i_fee = feelevel
replace i_fee = 0 if feeused == 3 

gen dataset = "GSS"

keep id dataset sample vpsu vstrat sampcode wtssnr wtssall wtall adults oversamp formwt r_* year month day svydate svydate2 dateintv day_of_week wkend week_of_month /*
*/ n_size isolated a?_* /*
*/ i_* intid 

keep if year == 1985 | year == 1986 | year == 1987 | year == 1998 | year == 2004 | year == 2014 

saveold "${dir_processed}/gss_data1.dta", replace version(12)

